<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 商品分类区域 -->
    <el-card v-loading='isLoading'>
      <el-button type="primary" style="margin-bottom:20px" @click="addCate">添加分类</el-button>
      <tree-table :data='cateList' :columns='columns' :expand-type='false' :selection-type='false' show-index
        index-text='#' style="margin-bottom:20px">
        <!-- 是否有效 -->
        <template #isok='{row}'>
          <i class="el-icon-success" v-if='row.cat_deleted === false' style="color:green"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!-- 操作 -->
        <template #btn>
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
          <el-button type="warning" size="mini" icon="el-icon-setting"></el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="info.pagenum"
        :page-sizes="[5,10,15]" :page-size="info.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="30%" @close='resetCateForm'>
      <!-- 表单 -->
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称: " prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类: ">
          <!-- 级联选择器 -->
          <el-cascader v-model="selectedKeys" :options="parentCateList"
            :props="{ expandTrigger: 'hover' , label:'cat_name' , checkStrictly: true , value:'cat_id'}"
            @change="handleChange" clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 父级分类下拉选中的值
      selectedKeys: [],
      // 父级分类数据
      parentCateList: [],
      // 添加分类数据校验
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 添加分类数据
      addCateForm: {
        cat_name: '',
        // 父级id
        cat_pid: 0,
        // 分类等级
        cat_level: 0
      },
      // 添加分类对话框显示状态
      addCateDialogVisible: false,
      // 是否在加载中
      isLoading: false,
      // 商品分类列表数据
      cateList: [],
      // 查询条件
      info: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 总数据条数
      total: 0,
      // 定义列表每一列数据
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 将当前列定义插槽
          type: 'template',
          // 插槽名称
          template: 'isok'
        },
        {
          label: '操作',
          // 将当前列定义插槽
          type: 'template',
          // 插槽名称
          template: 'btn'
        }
      ]
    }
  },
  methods: {
    // 添加分类确定按钮事件
    saveCate () {
      this.$refs.addCateFormRef.validate(async bool => {
        if (!bool) return console.log('校验失败')
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加失败')
        this.$message.success('添加成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
      this.addCateDialogVisible = false
    },
    // 添加分类对话框关闭时清空对话框数据
    resetCateForm () {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
    // 父级分类下拉值改变时触发的事件
    handleChange () {
      // 三种情况
      // 如果父级没选 pid = 0, level = 0
      // 父级选了一级 pid是一级id, level = 1
      // 父级选了二级 pid是二级id, level = 2
      if (this.selectedKeys.length === 0) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      } else {
        // if (this.selectedKeys.length === 1) {
        //   this.addCateForm.cat_pid = this.selectedKeys[0]
        //   this.addCateForm.cat_level = 1
        // } else if (this.selectedKeys.length === 2) {
        //   this.addCateForm.cat_pid = this.selectedKeys[1]
        //   this.addCateForm.cat_level = 2
        // }
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.cat_level = this.selectedKeys.length
      }
    },
    // 添加分类按钮点击事件
    async addCate () {
      // 获取父级分类数据
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类数据失败')
      this.parentCateList = res.data
      this.addCateDialogVisible = true
    },
    // 获取商品分类数据
    async getCateList () {
      this.isLoading = true
      const { data: res } = await this.$http.get('categories', { params: this.info })
      if (res.meta.status !== 200) console.log('获取数据失败')
      this.cateList = res.data.result
      this.total = res.data.total
      this.isLoading = false
    },
    // 改变分页显示条数触发
    handleSizeChange (newSize) {
      this.info.pagesize = newSize
      this.getCateList()
    },
    // 改变分页页码触发
    handleCurrentChange (num) {
      this.info.pagenum = num
      this.getCateList()
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getCateList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
